package com.wangyadong.hobby.thread;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.stream.IntStream;

public class LockTest {
    static Lock lock = new ReentrantLock();

    public static void main(String[] args) {
        IntStream.range(0, 100).forEach(i -> {
            Thread thread = new Thread(accRunnable());
            thread.start();
        });

    }


    private static Runnable accRunnable() {
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                try {
                    lock.lock();
                    System.out.println(Thread.currentThread().getName() + " \tviste");
                    TimeUnit.SECONDS.sleep(3);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } finally {
                    lock.unlock();
                }
            }
        };
        return runnable;
    }
}